package com.edu.solr_news.controller;

import com.edu.solr_news.bean.News;
import com.edu.solr_news.service.UserService;
import com.edu.solr_news.service.impl.NewsSearchServiceImpl;
import com.edu.solr_news.util.CountWordUtil;
import com.edu.solr_news.util.Pageutil;
import com.google.gson.Gson;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 响应层
 * @author ldy
 */

@Controller
public class NewsSearchController {
    @Autowired
    private NewsSearchServiceImpl newsSearchService;
    @Autowired
    private CountWordUtil countWordUtil;
    @Autowired
    private UserService userService;
    @Autowired
    private Gson gson;

    //登陆
    @GetMapping("/login")
    public String login(){
        return "page/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public String loginPost(String account,String password,HttpServletRequest request){

          return userService.login(account, password, request.getSession());

    }


    //首页
    @GetMapping({"","/","/index","/page/index"})
    public String  index(HttpServletRequest request, Model model)  {
        Pageutil schResult =new Pageutil();
        try {
            schResult = newsSearchService.hotNews("1", "5");
            Date date = new Date();
            SimpleDateFormat timeDf=new SimpleDateFormat("yyyy-MM-dd");
            String ciyunPath=newsSearchService.indexCiyun(request,timeDf.format(date),timeDf.format(date),"30");
            model.addAttribute("ciyunPath",ciyunPath);
        } catch (Exception e) {
            e.printStackTrace();
            return "error/500";
        }
        model.addAttribute("schResult",schResult);
        return "page/index";
    }

    //首页热词
    @GetMapping("/index/hotword")
    @ResponseBody
    public String indexhotword(String starttime,String endtime,String wordnum,HttpServletRequest request){
        String ciyunPath="";
        try {
        ciyunPath=newsSearchService.indexCiyun(request,starttime,endtime,wordnum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ciyunPath;
    }

    //首页图表
    @GetMapping("/index/data")
    @ResponseBody
    public String indexEchart(String starttime,String endtime){
        return newsSearchService.indexEchart(starttime,endtime);
    }


    //热点新闻
    @GetMapping("/page/new-hot")
    public String hotNews(String page,String limit,Model model){
        Pageutil schResult =new Pageutil();
        try {
            schResult = newsSearchService.hotNews(page, limit);
        } catch (Exception e) {
            e.printStackTrace();
            return "error/500";
        }
        model.addAttribute("schResult",schResult);
        return "page/hot-news";
    }

    //最新新闻
    @GetMapping("/page/new-news")
    public String newNews(String page,String limit,Model model)
    {
        Pageutil schResult=new Pageutil();
        try {
            schResult = newsSearchService.newNews(page, limit);
        } catch (Exception e) {
            e.printStackTrace();
            return "error/500";
        }
        model.addAttribute("schResult",schResult);
        return "page/new-news";

    }


   //新闻搜索
    @GetMapping("/page/new-search")
    public String search(String keyword, String page, String limit, String sort, Model model,HttpServletRequest request) {
        Pageutil schResult=new Pageutil();
        try {
            schResult = newsSearchService.search(keyword, page, limit, sort);
            HttpSession session=request.getSession();
            session.setAttribute("keyword",keyword);
            session.setAttribute("sort",sort);
        } catch (Exception e) {
            e.printStackTrace();
            return "error/500";
        }
       model.addAttribute("schResult",schResult);
        //默认来此请求该怎么处理
        return "page/search";
    }


    //新闻内容
    @GetMapping("/page/new-con")
    public String detail(String id,Model model){
        News news=new News();
        try {
             news= newsSearchService.searchById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "error/404";
        }
        Map<String, Integer> stringIntegerMap = countWordUtil.listword(news.getContent());
        model.addAttribute("news", news);
        model.addAttribute("keywords", stringIntegerMap);
        return "page/news-con";
    }


    //数据导出
    @GetMapping("/data/import")
    public String dataimport(String name,Model model){
        if(name.equals("search")){ model.addAttribute("url","search");}
        if(name.equals("new")) {model.addAttribute("url","new");}
        if(name.equals("hot")) {model.addAttribute("url","hot");}
        return "page/dataimport";
    }


    //导出数据获取
    @GetMapping("/data/imdata")
    @ResponseBody
    public String imdata(String name,String page,String limit,HttpServletRequest request) throws IOException, SolrServerException {
        Pageutil pageutil=new Pageutil();
      if(name.equals("search")){
          HttpSession session=request.getSession();
          String keyword= (String) session.getAttribute("keyword");
          String sort= (String) session.getAttribute("sort");
          pageutil=newsSearchService.searchForImport(keyword,page,limit,sort);
      }if(name.equals("new")){
          pageutil=newsSearchService.newNews(page,limit);
        } if(name.equals("hot")){
          pageutil=newsSearchService.hotNews(page,limit);
        }
      if(pageutil.getNews()!=null || pageutil.getNews().size()!=0){
        String json = gson.toJson(pageutil.getNews());
        return "{\n" +
                "  \"code\": 0,\n" +
                "  \"count\": " + String.valueOf(pageutil.getCount()) + ",\n" +
                "  \"data\": " + json + "}";
      }else {
          return "{\n" +
                  "  \"code\": 0,}";
      }
    }

}
